package com.demo.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DrawTree {
    static class Node {
        Node left;
        Node right;
        int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public static void pre() {

    }


    public static void main(String[] args) {
        Node node = buildTree(1);
        System.out.println(node.value);
        String str = "value";
    }

    private static Node buildTree(int k) {
        Node root = getN();
        while (k > 0) {
            Node n1= getN();
            Node n2= getN();
            buildTree(root, n1, n2);
            k--;
        }
        return root;
    }

    private static void buildTree(Node node, Node left, Node right) {
        node.left = left;
        node.right = right;
    }

    /**
     * 获取K层树
     *
     * @param k
     * @return
     */
    public static Node getKTree(int k) {
        Node root = getN();//随机获取一个跟节点
        if (k == 1) {
            return root;
        }
        List<Node> lastKNode = new ArrayList<Node>();
        lastKNode.add(root);
        for (int i = 2; i <= k; i++) {
            List<Node> nodes = getKNode(i, true);
            List<Node> ll = new ArrayList<Node>();
            ll.addAll(nodes);
            assKNodes(lastKNode, nodes);
            lastKNode.addAll(ll);
        }
        return root;
    }

    public static void assKNodes(List<Node> lNodes, List<Node> kNodes) {
        for (Node node : lNodes) {
            Node ln = popList(kNodes);
            Node rn = popList(kNodes);
            node.left = ln;
            node.right = rn;
        }
    }

    private static Node popList(List<Node> list) {
        int k = new Random().nextInt(list.size());
        Node node = list.get(k);//随机取一个树并且删除 方便下次取的收获不会重复取到
        list.remove(k);
        return node;
    }


    public static void assign(Node node, Node left, Node right) {
        node.left = left;
        node.right = right;
    }

    /**
     * 获取第K层的节点
     *
     * @param k
     */
    public static List<Node> getKNode(int k, boolean isFull) {
        int n = 1 << (k - 1);//第K层最大的节点数
        if (!isFull) {
            n = getV(n);//随机获取该层的节点
        }
        return getN(n);
    }

    /**
     * 获取N个节点
     *
     * @param n
     * @return
     */
    public static List<Node> getN(int n) {
        List<Node> nodes = new ArrayList<Node>();
        for (int i = 0; i < n; i++) {
            nodes.add(getN());
        }
        return nodes;
    }

    public static Node getN() {
        return new Node(getV(20));
    }

    private static int getV(int max) {
        int n = new Random().nextInt(max);
        System.out.println(n);
        return n;
    }

    private static int getV() {
        return new Random().nextInt();
    }


}
